//
//  BVHomeViewController.m
//  BuyVegetables
//
//  Created by 叶思超 on 15/10/9.
//  Copyright (c) 2015年 叶思超. All rights reserved.

#import "BVHomeViewController.h"
#import "BVConst.h"
#import "UIView+Extension.h"
#import "BVShopsCategoryDetail.h"
#import "BVShopsDetailCell.h"
#import "BVCorrectOrderViewController.h"
#import "BVLoginViewController.h"
#import "BVSearchShopsViewController.h"
#import "HttpTool.h"
#import "BVShopsCategoryDetail.h"
#import "BVShopsLeftCategory.h"
#import "BVShopsLeftCategoryDetail.h"
#import "BVShopNoticeDetail.h"
#import "MJExtension.h"
#import "UIImage+Extension.h"
#import "BVShopCart.h"
#import "BVShopCartCell.h"
#import "BVProvice.h"
#import "MBProgressHUD+MJ.h"
#import <BaiduMapAPI/BMapKit.h>
#import "MJRefresh.h"
#import "BVMyOrder.h"
#import "BVImageViewController.h"
#import "BVSearchDetailShopsViewController.h"

#import "UIButton+BVButtonAnimation.h" //购物车数量小图标的动画
// 左侧商品小分类高度
#define CategoryDetailH 40

@interface BVHomeViewController () <UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate, BMKLocationServiceDelegate, BVCorrectOrderViewControllerDelegate>
/**
 *  顶部公告信息scrollview
 */
@property (nonatomic, strong) UIScrollView *noticeScrollView;
/**
 *  顶部商品分类
 */
@property (nonatomic, strong) UIScrollView *shopsCategoryScrollView;
/**
 *  左侧商品分类小分类
 */
@property (nonatomic, strong) UIScrollView *shopsCategoryDetailScrollView;
/**
 *  顶部商品分类指示线
 */
@property (nonatomic, strong) UIView *accessoryLine;
/**
 *  选中button
 */
@property (nonatomic, strong) UIButton *selectedButton;
@property (nonatomic, strong) UIButton *selectedDetailButton;
/**
 *  商品详情tableview
 */
@property (nonatomic, strong) UITableView *tableView;
/**
 *  商品总价格
 */
@property (nonatomic, strong) UILabel *priceLabel;
/**
 *  存储所有商品
 */
@property (nonatomic, strong) NSMutableArray *shops;
/**
 *  存储提示信息
 */
@property (nonatomic, strong) NSMutableArray *notices;
/**
 *  左侧商品小分类
 */
@property (nonatomic, strong) NSMutableArray *leftCategoryShops;
/**
 *  商品详情
 */
@property (nonatomic, strong) NSMutableArray *shopsDetail;
/**
 *  添加定时器
 */
@property (nonatomic, strong) NSTimer *timer;
/**
 *  选中那个大分类标识
 */
@property (nonatomic, assign) NSUInteger selectedLagerCategoryFlag;
/**
 *  选中小分类标识
 */
@property (nonatomic, assign) NSUInteger selectedSmallCategoryFlag;
/**
 *  购物车tableView
 */
@property (nonatomic, strong) UITableView *shopCartTableView;
/**
 *  购物车商品数量
 */
@property (nonatomic, strong) UIButton *shopCartFlag;
/**
 *  购物车按钮
 */
@property (nonatomic, weak) UIButton *shopCartButton;
/**
 *  添加一个蒙板
 */
@property (nonatomic, strong) UIButton *coverButton;
/**
 *  购物车商品数量
 */
@property (nonatomic, strong) NSMutableArray *shopsCartCount;
/**
 *  去结算
 */
@property (nonatomic, strong) UIButton *balanceButton;
/**
 *  最低消费金额
 */
@property (nonatomic, copy) NSString *minMoney;
/**
 *  当前是第几页
 */
@property (nonatomic, assign) NSInteger currentPage;
/**
 *  存放所有城市信息
 */
@property (nonatomic, strong) NSArray *cities;
/**
 *  创建pickView
 */
@property (nonatomic, strong) UIPickerView *pickerView;
/**
 *  城市选择父视图
 */
@property (nonatomic, strong) UIView *citySelected;
/**
 *  省份名称
 */
@property (nonatomic, copy) NSString *proviceName;
/**
 *  城市名称
 */
@property (nonatomic, copy) NSString *cityName;
/**
 *  县
 */
@property(nonatomic, copy)NSString *xianName;
/**
 *  创建百度地图地位服务
 */
@property (nonatomic, strong) BMKLocationService *locationService;
@property (nonatomic, strong) CLGeocoder *geocoder;
/**
 *  公告提示
 */
@property (nonatomic, strong) UIImageView *noticeImage;
/**
 *  底部购物车
 */
@property (nonatomic, strong) UIView *bottomShopCart;

@property(nonatomic, strong)NSArray *citys;
@property(nonatomic, strong)NSArray *xians;

@end

@implementation BVHomeViewController{
    BVMyOrder *order;
}

static NSString *_ProviceName;
static NSString *_CityName;

- (CLGeocoder *)geocoder {
    if (!_geocoder) {
        _geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
    
}

- (NSMutableArray *)orders
{
    if (!_orders) {
        
        _orders = [NSMutableArray array];
        
    }
    
    return _orders;
}

- (NSArray *)cities {
    if (!_cities) {
       // _cities = [BVProvice objectArrayWithFilename:@"cities.plist"];
                
        NSMutableArray *arrayM = [NSMutableArray array];
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"json"];
        
        NSData *jdata = [[NSData alloc]initWithContentsOfFile:filePath];
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jdata options:0 error:nil];
        NSArray *array = dict[@"citylist"];
      
        for (NSDictionary *d in array) {
            
            BVProvice *model = [[BVProvice alloc] init];
            model.name = d[@"p"];
            
            NSMutableArray *cArrayM = [NSMutableArray array];
            for (NSDictionary *cd in d[@"c"]) {
                
                BVProvice *model2 = [[BVProvice alloc] init];
                model2.name = cd[@"n"];
                
                if(!cd[@"a"]){
                    model2.cities = [NSArray array];
                }else{
                    NSMutableArray *sArrayM = [NSMutableArray array];
                    for (NSDictionary *sdict in cd[@"a"]) {
                        BVProvice *mm = [[BVProvice alloc] init];
                        mm.name = sdict[@"s"];
                        [sArrayM addObject:mm];
                    }
                    model2.cities = sArrayM;
                }
                
                [cArrayM addObject:model2];

            }
             model.cities = cArrayM;
            
            [arrayM addObject:model];
        }
        _cities = arrayM;
        BVProvice *p = _cities[0];
        _citys = p.cities;
    }
    return _cities;
}

- (NSMutableArray *)shopsCartCount {
    if (!_shopsCartCount) {
        _shopsCartCount = [NSMutableArray array];
    }
    return _shopsCartCount;
}

- (UIButton *)coverButton {
    if (!_coverButton) {
        // 添加一个蒙板
        _coverButton = [[UIButton alloc] init];
        _coverButton.backgroundColor = [UIColor blackColor];
        _coverButton.alpha = 0.5;
        [_coverButton addTarget:self action:@selector(removeCoverClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _coverButton;
}

- (UIButton *)shopCartFlag {
    if (!_shopCartFlag) {
        _shopCartFlag = [[UIButton alloc] init];
        CGFloat shopCartFlagX = 15;
        CGFloat shopCartFlagy = -4;
        CGFloat shopCartFlagW = 15;
        CGFloat shopCartFlagH = 15;
        _shopCartFlag.frame = CGRectMake(shopCartFlagX, shopCartFlagy, shopCartFlagW, shopCartFlagH);
        _shopCartFlag.backgroundColor = BVBaseOrangeColor;
        _shopCartFlag.layer.cornerRadius = shopCartFlagW * 0.5;
        _shopCartFlag.layer.masksToBounds = YES;
        [_shopCartFlag setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_shopCartFlag addTarget:self action:@selector(shopCartButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _shopCartFlag.titleLabel.font = [UIFont systemFontOfSize:10];
        [self.shopCartButton addSubview:_shopCartFlag];
    }
    return _shopCartFlag;
}

- (NSMutableArray *)leftCategoryShops {
    if (!_leftCategoryShops) {
        _leftCategoryShops = [NSMutableArray array];
    }
    return _leftCategoryShops;
}

- (NSMutableArray *)shopsDetail {
    if (!_shopsDetail) {
        _shopsDetail = [NSMutableArray array];
    }
    return _shopsDetail;
}

- (NSMutableArray *)notices {
    if (!_notices) {
        _notices = [NSMutableArray array];
    }
    return _notices;
}

- (NSMutableArray *)shops {
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return _shops;
}

- (UITableView *)shopCartTableView {
    if (!_shopCartTableView) {
        _shopCartTableView = [[UITableView alloc] init];
        _shopCartTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _shopCartTableView.delegate = self;
        _shopCartTableView.dataSource = self;
        [self.view addSubview:_shopCartTableView];

        // 清空购物车
        UIView *deleteView = [[UIView alloc] init];
        deleteView.frame  = CGRectMake(0, 0, BVScreenW, 30);
        self.shopCartTableView.tableHeaderView = deleteView;

        UIImage *image = [UIImage imageNamed:@"wddd_del"];
        UIImageView *deletedImage = [[UIImageView alloc] init];
        deletedImage.frame = CGRectMake(BVScreenW - 20 - image.size.width, (deleteView.height - image.size.height) * 0.5, image.size.width, image.size.height);
        deletedImage.image = image;
        [deleteView addSubview:deletedImage];

        // 提示语
        UILabel *noticeLabel = [[UILabel alloc] init];
        noticeLabel.frame = CGRectMake(0, 0, deletedImage.x - 10, 30);
        noticeLabel.text = @"清空购物车";
        noticeLabel.font = [UIFont systemFontOfSize:14];
        noticeLabel.textAlignment = NSTextAlignmentRight;
        [deleteView addSubview:noticeLabel];

        UIButton *deletedButton = [[UIButton alloc] init];
        deletedButton.frame = CGRectMake(0, 0, BVScreenW, 30);
        deletedButton.adjustsImageWhenHighlighted = NO;
        deletedButton.titleLabel.font = [UIFont systemFontOfSize:14];
        deletedButton.contentHorizontalAlignment  = NSTextAlignmentRight;
        deletedButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 50);
        deletedButton.imageEdgeInsets = UIEdgeInsetsMake(0, 200, 0, 0);
        [deletedButton addTarget:self action:@selector(deletedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [deletedButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [deleteView addSubview:deletedButton];
    }
    return _shopCartTableView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(CGRectGetMaxX(self.shopsCategoryDetailScrollView.frame), CGRectGetMaxY(self.shopsCategoryScrollView.frame), BVScreenW - CGRectGetMaxX(self.shopsCategoryDetailScrollView.frame), BVScreenH -  CGRectGetMaxY(self.shopsCategoryScrollView.frame) - 40);
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 160.8;
        _tableView.pagingEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (UIScrollView *)shopsCategoryDetailScrollView {
    if (!_shopsCategoryDetailScrollView) {
        NSInteger categotyCount = 0;
        
        if (BVScreenH == 736) { // 6p
            categotyCount = 6;
        } else if (BVScreenH == 667) { // 6
            categotyCount = 5;
        } else if (BVScreenH == 568) { // 5/5s
            categotyCount = 5;
        } else if (BVScreenH == 480) { // 4/4s
            categotyCount = 4;
        }
        
        CGFloat shopCategoryW = BVScreenW / categotyCount;

        _shopsCategoryDetailScrollView = [[UIScrollView alloc] init];
        _shopsCategoryDetailScrollView.frame = CGRectMake(0, CGRectGetMaxY(self.shopsCategoryScrollView.frame), shopCategoryW, BVScreenH -  CGRectGetMaxY(self.shopsCategoryScrollView.frame) - 40);
        _shopsCategoryDetailScrollView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_shopsCategoryDetailScrollView];
    }
    return _shopsCategoryDetailScrollView;
}

/**
 *  购物车商品列表滑动视图
 */
- (UIScrollView *)shopsCategoryScrollView {
    if (!_shopsCategoryScrollView) {
        self.shopsCategoryScrollView = [[UIScrollView alloc] init];
        self.shopsCategoryScrollView.frame = CGRectMake(0, CGRectGetMaxY(self.noticeScrollView.frame), BVScreenW, 40);
        self.shopsCategoryScrollView.bounces = NO;
        self.shopsCategoryScrollView.backgroundColor = BVColor(233, 234, 235);
        self.shopsCategoryScrollView.contentSize = CGSizeMake(BVScreenW * 5, 0);
        self.shopsCategoryScrollView.showsVerticalScrollIndicator = NO;
        self.shopsCategoryScrollView.showsHorizontalScrollIndicator = NO;
        self.shopsCategoryScrollView.delegate = self;
        [self.view addSubview:self.shopsCategoryScrollView];
    }
    return _shopsCategoryScrollView;
}
/**
 *  顶部公告滑动列表视图
 */
- (UIScrollView *)noticeScrollView {
    if (!_noticeScrollView) {
        self.noticeScrollView = [[UIScrollView alloc] init];
        self.noticeScrollView.frame = CGRectMake(0, 64, BVScreenW, 40);
        self.noticeScrollView.backgroundColor = BVTextLightGrayColor;
        self.noticeScrollView.showsHorizontalScrollIndicator = NO;
        self.noticeScrollView.showsVerticalScrollIndicator = NO;
        self.noticeScrollView.pagingEnabled = YES;
        self.noticeScrollView.delegate = self;
        [self.view addSubview:self.noticeScrollView];
    }
    return _noticeScrollView;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _locationService.delegate = self;

    [self loadMyOrderData];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _locationService.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _citys = [NSArray array];
    _xians = [NSArray array];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cleanShopCar:) name:PayFinishNotification object:nil];
    
    // 初始化
    self.selectedLagerCategoryFlag = 0;
    self.selectedSmallCategoryFlag = 0;


    // 百度地图定位
    [self setupBMKLocation];

    // 设置导航栏
    [self setupNav];
    
    // 添加定时器
    [self addTimer];
    
    // 获取最低消费金额
    [self loadMinMoneyData];
    
    [self loadUserInfo];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableViewAndBaseCart) name:ChangeShopCluntNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllShop) name:@"removeAllShop" object:nil];
}

- (void)removeAllShop{
    
    for (NSInteger i = 0; i < self.shopsCartCount.count; i++) {
        BVShopsLeftCategoryDetail *shopsLeftCategoryDetail = self.shopsCartCount[i];
        [BVShopCart deleteShopFromCart:shopsLeftCategoryDetail];
    }
    
    [self setupBaseInfo];
    [self.tableView reloadData];
}

#pragma mark -- 获取用户信息
- (void)loadUserInfo {
    // 拼接路径
    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/getUserInfoById", Path];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSUserDefaults *defaults    = [NSUserDefaults standardUserDefaults];
    NSDictionary *userinfo = [defaults objectForKey:@"USERINFO"];
    NSString *user_id = userinfo[@"id"];
    params[@"user_id"] = user_id;
    if (user_id != nil) {
        [HttpTool get:url params:params success:^(id responseObj) {
            if ([responseObj[@"code"] isEqualToString:@"1"]) {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:responseObj[@"data"] forKey:@"person"];
                
            } else {

            }
        } failure:^(NSError *error) {
            BVLog(@"请求失败%@", error);
        }];
    }
}

#pragma mark -- 百度地图地位
- (void)setupBMKLocation {
    self.locationService = [[BMKLocationService alloc] init];
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [BMKLocationService setLocationDistanceFilter:100.f];
    self.locationService.delegate = self;
    LOCATING
    [self.locationService startUserLocationService];
}

/**
 *   添加定时器
 */
- (void)addTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextnotice) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

/**
 *  移除定时器
 */
- (void)removeTimer {
    [self.timer invalidate];
    self.timer = nil;
}

// 下张图片
- (void)nextnotice {
    // 增加pageCurrent的页数
    NSInteger pageCurrent = 0;
    if (self.currentPage == self.notices.count - 1) { // 最后一张
    pageCurrent = 0;
    } else { // 下一页
    pageCurrent = self.currentPage + 1;
    }

    // 计算scrollView的位置
    CGFloat offsetX = pageCurrent * BVScreenW;
    CGPoint offset = CGPointMake(offsetX, 0);
    [self.noticeScrollView setContentOffset:offset animated:YES];
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.noticeScrollView == scrollView) {
        int page = (self.noticeScrollView.contentOffset.x + BVScreenW * 0.5) / BVScreenW;
        self.currentPage = page;
    } else if (self.shopsCategoryScrollView == scrollView) {
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (self.noticeScrollView == scrollView) {
        // 停止定时器
        [self removeTimer];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.noticeScrollView == scrollView) {
        // 开启定时器
        [self addTimer];
    }
}

#pragma mark -- 获取最低消费
- (void)loadMinMoneyData {

    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/getMinMoneyInfo", Path];
    [HttpTool get:url params:nil success:^(id responseObj) {
        if ([responseObj[@"code"] isEqualToString:@"1"]) {
            self.minMoney = responseObj[@"data"][@"money"];
            // 底部购物车
            [self setupBottomShopCart];
        } else {
            ERROR(@"获取最低消费失败");
        }
    } failure:^(NSError *error) {
        NETWORKINGERROR
    }];
}

#pragma mark -- 加载商品数据
- (void)loadShopsData {
    if(self.citySelected){
        [self.citySelected removeFromSuperview];
        self.citySelected = nil;
    }
     //拼接路径
    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/getVegetableInfoByArea", Path];
    
    // 拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (_ProviceName == nil || _CityName == nil) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        NSString *provice = [defaults stringForKey:@"provice"];
        NSString *city = [defaults stringForKey:@"city"];

        if (provice != nil && city != nil && self.proviceName == nil && self.cityName == nil) {
        self.proviceName = provice;
        self.cityName = city;
        }
        params[@"province"] = self.proviceName;
        params[@"city"] = self.cityName;
        

        if(self.xianName){
            params[@"dist"] = self.xianName;
        }
        
    } else {
        params[@"province"] = _ProviceName;
        params[@"city"] = _CityName;
        if(_xianName){
            params[@"dist"] = self.xianName;
        }
    }

    // 取出大分类ID
    if (self.shops.count != 0) {
       BVShopsCategoryDetail *shopsDetail = self.shops[self.selectedLagerCategoryFlag];
       params[@"parent_id"] = shopsDetail.ID;
    }
    // 子分类
    if (self.shops.count != 0) {
        BVShopsCategoryDetail *shopsDetail = self.shops[self.selectedLagerCategoryFlag];
        BVShopsLeftCategory *leftCategory  = shopsDetail.child_list[self.selectedSmallCategoryFlag];
        params[@"child_id"] = leftCategory.ID;
        params[@"parent_id"] = leftCategory.parent_id;
    }
    
    NSDictionary * userinfo =[[NSUserDefaults standardUserDefaults] objectForKey:@"USERINFO"];
    if (userinfo[@"id"] != nil) {
        params[@"user_id"] = userinfo[@"id"];
    }else{
        params[@"user_id"] = @"";
    }
    
    SHOWLOADING
    [HttpTool get:url params:params success:^(id responseObj) {
        BVLog(@"----------//----------%@",responseObj);
        
        HIDELOADING
        if ([responseObj[@"code"] isEqualToString:@"1"]) {
            [self.shops removeAllObjects];
            // 显示底部购物车
            self.bottomShopCart.hidden = NO;

            NSDictionary *dict = responseObj[@"data"];
            
            // 提醒商品
            NSArray *noticeArray = [BVShopNoticeDetail objectArrayWithKeyValuesArray:dict[@"notice_list"]];
            [self.notices addObjectsFromArray:noticeArray];
            [self setupNotice];

            // 商品信息
            NSArray *shopsArray = [BVShopsCategoryDetail objectArrayWithKeyValuesArray:dict[@"parent_list"]];
            [self.shops addObjectsFromArray:shopsArray];
            
            // 显示字空间
            self.noticeScrollView.hidden = NO;

            self.shopsCategoryScrollView.hidden = NO;

            self.shopsCategoryDetailScrollView.hidden = NO;

            self.tableView.hidden = NO;

            self.noticeImage.hidden = NO;

            [self setupShops];
        } else {
            // 隐藏控件
            self.noticeScrollView.hidden = YES;
            self.bottomShopCart.hidden = YES;

            for (id obj in self.shopsCategoryScrollView.subviews) {
                [obj removeFromSuperview];
            }

            self.shopsCategoryScrollView.hidden = YES;

            self.shopsCategoryDetailScrollView.hidden = YES;

            self.tableView.hidden = YES;

            self.noticeImage.hidden = YES;

            ERROR(@"当前城市暂未开通");
        }
    } failure:^(NSError *error) {
        HIDELOADING
        NETWORKINGERROR
    }];
}
#pragma mark ---------------------------------------
#pragma mark --------------------------左侧分类详情和具体商品信息-------------------------------
- (void)loadLeftShopsDetailData {
    
    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/getVegetableInfoByArea", Path];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (_ProviceName == nil || _CityName == nil) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *provice = [defaults stringForKey:@"provice"];
        NSString *city = [defaults stringForKey:@"city"];
        if (provice != nil && city != nil && self.proviceName == nil && self.cityName == nil) {
        self.proviceName = provice;
        self.cityName = city;
        }
        params[@"province"] = self.proviceName;
        params[@"city"] = self.cityName;
        
        NSString *dist = self.xianName;

        
        if(dist && ![dist isEqualToString:@""]){
            params[@"dist"] = dist;
        }
    } else {
        params[@"province"] = _ProviceName;
        params[@"city"] = _CityName;
        
        if(_xianName){
            params[@"dist"] = _xianName;
        }
    }
    
    // 取出大分类ID
    if (self.shops.count != 0) {
        BVShopsCategoryDetail *shopsDetail = self.shops[self.selectedLagerCategoryFlag];
        params[@"parent_id"] = shopsDetail.ID;
    }
    
    // 子分类
    if (self.shops.count != 0) {
        BVShopsCategoryDetail *shopsDetail = self.leftCategoryShops[self.selectedLagerCategoryFlag];
        BVShopsLeftCategory *leftCategory  = shopsDetail.child_list[self.selectedSmallCategoryFlag];
        params[@"child_id"] = leftCategory.ID;
        params[@"parent_id"] = leftCategory.parent_id;
    }
    NSDictionary * userinfo =[[NSUserDefaults standardUserDefaults] objectForKey:@"USERINFO"];
    if (userinfo[@"id"] != nil) {
        params[@"user_id"] = userinfo[@"id"];
    }else{
        params[@"user_id"] = @"";
    }
    
    SHOWLOADING
    BVLog(@"----------------%@",params[@"dist"]);
    [HttpTool get:url params:params success:^(id responseObj) {
        BVLog(@"%@",params);
        BVLog(@"=======<<<----------%@",responseObj);
        
        HIDELOADING
        [self.shopsDetail removeAllObjects];
        if ([responseObj[@"code"] isEqualToString:@"1"]) {
            
            NSDictionary *dict  = responseObj[@"data"];
            // 商品信息
            NSArray *shopsArray = [BVShopsCategoryDetail objectArrayWithKeyValuesArray:dict[@"parent_list"]];
            [self.shopsDetail addObjectsFromArray:shopsArray];

        } else {
            ERROR(@"数据获取失败");
        }
        // 结束刷新
        [self.tableView headerEndRefreshing];
        // 刷新数据
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        HIDELOADING
        NETWORKINGERROR
        [self.tableView headerEndRefreshing];
    }];
}

#pragma mark -- 加载左侧分类数据
- (void)loadLeftShopsData {
    
    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/getVegetableInfoByArea", Path];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (_ProviceName == nil || _CityName == nil) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *provice = [defaults stringForKey:@"provice"];
        NSString *city = [defaults stringForKey:@"city"];
        if (provice != nil && city != nil && self.proviceName == nil && self.cityName == nil) {
        self.proviceName = provice;
        self.cityName = city;
        }
        params[@"province"] = self.proviceName;
        params[@"city"] = self.cityName;
        
        NSString *dist = self.xianName;
        
        if(dist && ![dist isEqualToString:@""]){
            params[@"dist"] = dist;
        }
    } else {
        params[@"province"] = _ProviceName;
        params[@"city"] = _CityName;
        if(_xianName){
            params[@"dist"] = _xianName;
        }
    }
    
    // 取出大分类ID
    if (self.shops.count != 0) {
        BVShopsCategoryDetail *shopsDetail = self.shops[self.selectedLagerCategoryFlag];
        params[@"parent_id"] = shopsDetail.ID;
    }
    
    NSDictionary * userinfo =[[NSUserDefaults standardUserDefaults] objectForKey:@"USERINFO"];
    if (userinfo[@"id"] != nil) {
        params[@"user_id"] = userinfo[@"id"];
    }else{
        params[@"user_id"] = @"";
    }
    
    SHOWLOADING
    [HttpTool get:url params:params success:^(id responseObj) {
        HIDELOADING
        BVLog(@"======<<<>>>>>>%@",responseObj);
        
        if ([responseObj[@"code"] isEqualToString:@"1"]) {
            
            [self.leftCategoryShops removeAllObjects];
            [self.tableView reloadData];
            NSDictionary *dict  = responseObj[@"data"];

            // 商品信息
            NSArray *shopsArray = [BVShopsCategoryDetail objectArrayWithKeyValuesArray:dict[@"parent_list"]];
            [self.leftCategoryShops addObjectsFromArray:shopsArray];
            // 清空子控件
            for (UIButton *btn in self.shopsCategoryDetailScrollView.subviews) {
                [btn removeFromSuperview];
            }
            // 商品分类小分类
            [self setupShopsDetail];
        } else {
            ERROR(@"数据获取失败");
        }
    } failure:^(NSError *error) {
        HIDELOADING
        NETWORKINGERROR
    }];
}
#pragma mark -- 商品分类小分类
- (void)setupShopsDetail {
    // 左侧商品分类
    BVShopsCategoryDetail *shopsCategoryDetail = self.leftCategoryShops[self.selectedLagerCategoryFlag];
    // 分类个数
    NSUInteger leftCategoryCount = shopsCategoryDetail.child_list.count;

    // scrollview的contentSize
    self.shopsCategoryDetailScrollView.contentSize = CGSizeMake(0, CategoryDetailH * leftCategoryCount);
    
    for (NSInteger i = 0; i < leftCategoryCount; i++) {
        BVShopsLeftCategory *shopsLeftCategory = shopsCategoryDetail.child_list[i];
        UIButton *shopsCategoryDetailButton = [[UIButton alloc] init];
        shopsCategoryDetailButton.x = 0;
        shopsCategoryDetailButton.y = i * CategoryDetailH;
        shopsCategoryDetailButton.width = self.shopsCategoryDetailScrollView.width;
        shopsCategoryDetailButton.height = CategoryDetailH;
        shopsCategoryDetailButton.titleLabel.numberOfLines   = 2;
        shopsCategoryDetailButton.tag = 100 + i;
        shopsCategoryDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        shopsCategoryDetailButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [shopsCategoryDetailButton setTitle:shopsLeftCategory.name forState:UIControlStateNormal];
        [shopsCategoryDetailButton setTitleColor:BVBaseTextBlackColor forState:UIControlStateNormal];
        [shopsCategoryDetailButton setTitleColor:BVBaseOrangeColor forState:UIControlStateSelected];
        [shopsCategoryDetailButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected];
        [shopsCategoryDetailButton addTarget:self action:@selector(shopsCategoryDetailDidChangeClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.shopsCategoryDetailScrollView addSubview:shopsCategoryDetailButton];
        if (0 == i) {
            [self shopsCategoryDetailDidChangeClick:shopsCategoryDetailButton];
        }
    }
    
    // 添加刷新
    [self.tableView addHeaderWithTarget:self action:@selector(loadLeftShopsDetailData)];
}

#pragma mark -- 商品分类
- (void)setupShops {
    // 顶部商品分类
    NSUInteger shopsCategoryCount = self.shops.count;

    NSInteger categotyCount       = 0;
    
    if (BVScreenH == 736) { // 6p
        categotyCount = 6;
    } else if (BVScreenH == 667) { // 6
        categotyCount = 5;
    } else if (BVScreenH == 568) { // 5/5s
        categotyCount = 5;
    } else if (BVScreenH == 480) { // 4/4s
        categotyCount = 4;
    }
    
    CGFloat shopCategoryW = BVScreenW / categotyCount;

    // scrollview的contentSize大小
    CGFloat scrollW = shopCategoryW * shopsCategoryCount;
    self.shopsCategoryScrollView.contentSize = CGSizeMake(scrollW, 0);

    // 底部分割线
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.frame = CGRectMake(0, self.shopsCategoryScrollView.height - 2, scrollW, 2);
    bottomLine.backgroundColor = BVColor(42, 42, 42);
    [self.shopsCategoryScrollView addSubview:bottomLine];
    
    for (UIView *v in self.shopsCategoryScrollView.subviews) {
        [v removeFromSuperview];
    }
    
    for (NSUInteger i = 0; i < shopsCategoryCount; i++) {
        BVShopsCategoryDetail *shopsCategoryDetail = self.shops[i];

        UIButton *shopsCategoryButton = [[UIButton alloc] init];
        shopsCategoryButton.x = shopCategoryW * i;
        shopsCategoryButton.y = 0;
        shopsCategoryButton.width = shopCategoryW;
        shopsCategoryButton.height = self.shopsCategoryScrollView.height;
        shopsCategoryButton.tag = i;
        shopsCategoryButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [shopsCategoryButton setTitle:shopsCategoryDetail.name forState:UIControlStateNormal];
        [shopsCategoryButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [shopsCategoryButton addTarget:self action:@selector(shopsCategoryDidChangeClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.shopsCategoryScrollView addSubview:shopsCategoryButton];
        
        if (self.selectedLagerCategoryFlag == i) {
            self.accessoryLine = [[UIView alloc] init];
            self.accessoryLine.frame = CGRectMake(0, self.shopsCategoryScrollView.height - 2, shopCategoryW, 2);
            self.accessoryLine.backgroundColor = BVColor(223, 90, 89);
            [shopsCategoryButton addSubview:self.accessoryLine];

            [self shopsCategoryDidChangeClick:shopsCategoryButton];
        }
        
        if (i > 0 && i < shopsCategoryCount) {
            UIView *separatorLine = [[UIView alloc] init];
            separatorLine.x = shopsCategoryButton.x - 0.5;
            separatorLine.y = 10;
            separatorLine.width = 1;
            separatorLine.height = shopsCategoryButton.height - separatorLine.y * 2;
            separatorLine.backgroundColor = [UIColor blackColor];
            separatorLine.alpha = 0.15;
            [self.shopsCategoryScrollView addSubview:separatorLine];
        }
    }
}

#pragma mark -- 设置公告
- (void)setupNotice {
    NSInteger noticesCount = self.notices.count;
    self.noticeScrollView.contentSize = CGSizeMake(BVScreenW * noticesCount, 0);

    // 顶部公告信息
    UIImageView *noticeImage = [[UIImageView alloc] init];
    noticeImage.frame = CGRectMake(10, 10 + 64, 20, self.noticeScrollView.height - 20);
    noticeImage.image = [UIImage imageNamed:@"sy_notice"];
    [self.view addSubview:noticeImage];
    self.noticeImage = noticeImage;
    
    for (NSInteger i = 0; i < noticesCount; i++) {
        BVShopNoticeDetail *noticeDetail = self.notices[i];

        UIButton *noticeButton = [[UIButton alloc] init];
        noticeButton.frame = CGRectMake(CGRectGetMaxX(noticeImage.frame) + 10 + BVScreenW * i, 0, BVScreenW - noticeImage.width - 10, self.noticeScrollView.height);
        [noticeButton setTitle:noticeDetail.content forState:UIControlStateNormal];
        [noticeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        noticeButton.titleLabel.font = [UIFont systemFontOfSize:15];
        noticeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.noticeScrollView addSubview:noticeButton];
    }
}

#pragma mark -- 设置导航栏
- (void)setupNav {
    // 导航栏基本设置
    self.navigationController.navigationBar.barTintColor = BVBaseOrangeColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"城市" style:UIBarButtonItemStyleDone target:self action:@selector(cityChangeClick)];

    // 搜索框
    UIButton *searchButton = [[UIButton alloc] init];
    searchButton.frame = CGRectMake(44, 35, BVScreenW - 44 * 2, 35);
    searchButton.backgroundColor = [UIColor clearColor];
    searchButton.titleLabel.font = [UIFont systemFontOfSize:14];

    [searchButton setImage:[UIImage imageNamed:@"sy_search"] forState:UIControlStateNormal];
    [searchButton setTitle:@"请输入菜品名称" forState:UIControlStateNormal];
    [searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    searchButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [searchButton addTarget:self action:@selector(searchShopsClick) forControlEvents:UIControlEventTouchUpInside];
    searchButton.layer.masksToBounds = YES;
    searchButton.layer.cornerRadius = 4;
    searchButton.layer.borderWidth = 0.5;
    searchButton.layer.borderColor =[[UIColor whiteColor] CGColor];
    self.navigationItem.titleView = searchButton;
}

#pragma mark -- 购物车
- (void)setupBottomShopCart {
    // 购物车父视图
    UIView *bottomShopCart = [[UIView alloc] init];
    bottomShopCart.frame = CGRectMake(0, BVScreenH - 40,  BVScreenW, 40);
    bottomShopCart.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, BVScreenW, 0.5);
    label.backgroundColor = BVBaseTextLightBlackColor;
    [bottomShopCart addSubview:label];
    [self.view addSubview:bottomShopCart];
    self.bottomShopCart = bottomShopCart;
    self.bottomShopCart.hidden = YES;

    // 左侧购物车按钮
    UIButton *shopCartButton = [[UIButton alloc] init];
    shopCartButton.frame = CGRectMake(10, 8, 24, 24);
    shopCartButton.backgroundColor = [UIColor clearColor];
    shopCartButton.enabled = NO;
    [shopCartButton setImage:[UIImage imageNamed:@"gwc"] forState:UIControlStateNormal];
    [shopCartButton addTarget:self action:@selector(shopCartButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomShopCart addSubview:shopCartButton];
    self.shopCartButton = shopCartButton;
    [self.shopCartButton addSubview:self.shopCartFlag];

    // 总价格
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.textColor = BVBaseOrangeColor;
    self.priceLabel.frame = CGRectMake(CGRectGetMaxX(shopCartButton.frame) + 20, 8, BVScreenW * 0.7 - CGRectGetMaxX(shopCartButton.frame) - 10, shopCartButton.height);
    self.priceLabel.text = @"¥0";
    self.priceLabel.font = [UIFont systemFontOfSize:15];
    [bottomShopCart addSubview:self.priceLabel];

    // 右侧结算按钮
    UIButton *balanceButton = [[UIButton alloc] init];
    balanceButton.frame = CGRectMake(BVScreenW * 0.7, 0, BVScreenW * 0.3, bottomShopCart.height);
    balanceButton.enabled = NO;
    [balanceButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [balanceButton setTitle:[NSString stringWithFormat:@"还差%@元", self.minMoney] forState:UIControlStateNormal];
    balanceButton.titleLabel.font = [UIFont systemFontOfSize:15];
    balanceButton.backgroundColor = BVColor(153, 153, 153);
    [balanceButton addTarget:self action:@selector(correctOrderClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomShopCart addSubview:balanceButton];
    self.balanceButton = balanceButton;

    // 设置购物车基本信息
    [self setupBaseInfo];
}

#pragma mark -- UITabelViewDelegate-UITabelViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableView) {
        // 左侧商品分类
        BVShopsCategoryDetail *shopsCategoryDetail = self.shopsDetail[self.selectedLagerCategoryFlag];
        // 左侧分类
        BVShopsLeftCategory *leftCategory = shopsCategoryDetail.child_list[self.selectedSmallCategoryFlag];
        BVShopsLeftCategoryDetail *detail = leftCategory.breed_list[indexPath.row];
        
        //如果不是限购就返回160的高度 如果是限购就返回180的高度
        if (detail.isxg == 2) {
            return 180;
        }else{
            return 160;
        }
    }else{
        
        return 30;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView) {
        if (self.shopsDetail.count == 0) return 0;
        // 左侧商品分类
        BVShopsCategoryDetail *shopsCategoryDetail = self.shopsDetail[self.selectedLagerCategoryFlag];
        // 左侧分类
        BVShopsLeftCategory *leftCategory = shopsCategoryDetail.child_list[self.selectedSmallCategoryFlag];

        return leftCategory.breed_list.count;

    } else {

        return self.shopsCartCount.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableView) {
    
        BVShopsDetailCell *cell = [BVShopsDetailCell cellWithTableView:tableView];
        // 左侧商品分类
        BVShopsCategoryDetail *shopsCategoryDetail = self.shopsDetail[self.selectedLagerCategoryFlag];
        // 左侧分类
        BVShopsLeftCategory *leftCategory = shopsCategoryDetail.child_list[self.selectedSmallCategoryFlag];
        // 增加商品按钮
        cell.addButton.tag = indexPath.row + 200;
        [cell.addButton addTarget:self action:@selector(addShops:) forControlEvents:UIControlEventTouchUpInside];
        
        // 减少商品
        cell.subButton.tag = indexPath.row + 500;
        [cell.subButton addTarget:self action:@selector(subShops:) forControlEvents:UIControlEventTouchUpInside];

        //收藏商品
        [cell.collecButton setImage:[UIImage imageNamed:@"discollection"] forState:UIControlStateNormal];
        [cell.collecButton addTarget:self action:@selector(collecBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        BVShopsLeftCategoryDetail *detailShops = leftCategory.breed_list[indexPath.row];
        cell.collecButton.tag = indexPath.row + 380;
        
        if ([detailShops.is_collect isEqualToString:@"1"]) {
            [cell.collecButton setImage:[UIImage imageNamed:@"collection"] forState:UIControlStateNormal];
        } else {
            [cell.collecButton setImage:[UIImage imageNamed:@"discollection"] forState:UIControlStateNormal];
        }
        // 传第模型
        cell.shopsDetail = leftCategory.breed_list[indexPath.row];
        
        return cell;
        
    } else {
        // 创建cell
        BVShopCartCell *cell = [BVShopCartCell cell];

        // 增加商品
        cell.addBtn.tag = 300 + indexPath.row;
        [cell.addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        // 减少商品数量
        cell.subBtn.tag = 600 + indexPath.row;
        [cell.subBtn addTarget:self action:@selector(subBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        // 传递数据模型
        cell.shopsDetail = self.shopsCartCount[indexPath.row];
        
        return cell;
    }
}
#pragma 收藏按钮点击事件
- (void)collecBtnClick:(UIButton *)button{
    
    BVShopsCategoryDetail *shopsCategoryDetail = self.shopsDetail[self.selectedLagerCategoryFlag];
    BVShopsLeftCategory *leftCategory = shopsCategoryDetail.child_list[self.selectedSmallCategoryFlag];
    BVShopsLeftCategoryDetail *detailShops = leftCategory.breed_list[button.tag - 380];
    BVLog(@"商品ID是:%@",detailShops.ID);
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *url;
    NSDictionary * userinfo =[[NSUserDefaults standardUserDefaults] objectForKey:@"USERINFO"];
    params[@"user_id"] = userinfo[@"id"];
    params[@"goods_id"] = detailShops.ID;
    
    if (userinfo[@"id"] == nil) {
        BVLoginViewController *loginVC = [[BVLoginViewController alloc] init];
        ROOTNAVPUSH(loginVC, YES);
        return;
    }
    
    if ([detailShops.is_collect isEqualToString:@"1"]) {
        //取消收藏
        url = [NSString stringWithFormat:@"%@/VegetableUserAPI/deleteCollect", Path];
        [HttpTool get:url params:params success:^(id responseObj) {
            if ([responseObj[@"code"] isEqualToString:@"0"]) {
                 //按钮动画
                [self addAnimationForCollecBtn:button];
                [button setImage:[UIImage imageNamed:@"discollection"] forState:UIControlStateNormal];
                detailShops.is_collect = @"0";
                [self showMessage:@"取消收藏成功!"];
            } else {
                [self showMessage:responseObj[@"msg"]];
            }
        } failure:^(NSError *error) {
            [self showMessage:@"取消收藏失败!"];
        }];
    } else {
        url = [NSString stringWithFormat:@"%@/VegetableUserAPI/addCollect", Path];
        [HttpTool get:url params:params success:^(id responseObj) {
            if ([responseObj[@"code"] isEqualToString:@"0"]) {
               
                [self addAnimationForCollecBtn:button];
                [button setImage:[UIImage imageNamed:@"collection"] forState:UIControlStateNormal];
                detailShops.is_collect = @"1";
            }else {
                [self showMessage:responseObj[@"msg"]];
            }
        } failure:^(NSError *error) {
            BVLog(@"%@",error.description);
        }];
    }
}
#pragma mark - 对购物车数量按钮进行动画
- (void)addAnimationForCollecBtn:(UIButton *)button{
    button.transform = CGAffineTransformIdentity;
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:0 animations: ^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 / 3.0 animations: ^{
            button.transform = CGAffineTransformMakeScale(1.5, 1.5);
        }];
        [UIView addKeyframeWithRelativeStartTime:1/3.0 relativeDuration:1/3.0 animations: ^{
            button.transform = CGAffineTransformMakeScale(0.8, 0.8);
        }];
        [UIView addKeyframeWithRelativeStartTime:2/3.0 relativeDuration:1/3.0 animations: ^{
            button.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
    } completion:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        
        BVShopsCategoryDetail *shopsCategoryDetail = self.shopsDetail[self.selectedLagerCategoryFlag];
        
        // 左侧分类
        BVShopsLeftCategory *leftCategory = shopsCategoryDetail.child_list[self.selectedSmallCategoryFlag];
        
        BVShopsLeftCategoryDetail *shopsDetail = leftCategory.breed_list[indexPath.row];
        
        BVImageViewController *imgVC = [BVImageViewController new];
        
        imgVC.imgName = shopsDetail.pic_name;
        
        [self.navigationController pushViewController:imgVC animated:YES];
    }else{
    
    }
}

#pragma mark - UIPickerViewDataSource
// 告诉系统有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

// 告诉系统有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (0 == component) {
        return self.cities.count;
    }else if(component == 1){
        return _citys.count;
    }else{
        return _xians.count;
    }
}

#pragma mark - UIPickerViewDelegate
// 告诉系统每一行显示什么内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (0 == component) {
        // 省份
        // 1.获取对应行对应的省份模型
        BVProvice *province   = self.cities[row];
        // 2.返回省份的名称
        return province.name;
        
    } else if(component == 1){
         BVProvice *model = self.citys[row];
        return model.name;
    }else{
        BVProvice *model = self.xians[row];
        return model.name;
    }
}

// 监听pickerView的选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // 判断是否修改了第0列(省份列)
    if (0 == component) {
        // 刷新第1列对应的数据
        BVProvice *model = self.cities[row];
        _citys = model.cities;
        
        [pickerView reloadComponent:1];
        // 让第1列滚动到第0行
        [pickerView selectRow:0 inComponent:1 animated:YES];
        
        if(_citys.count >0){
            BVProvice *model1 = _citys[0];
            _xians = model1.cities;
        }else{
            _xians = [NSArray array];
        }
       
        [pickerView reloadComponent:2];
        
    }else if(1 == component){
        BVProvice *model = _citys[row];
        _xians = model.cities;
        [pickerView reloadComponent:2];
        // 让第1列滚动到第0行
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
}
#pragma mark -
#pragma mark - 切换城市
- (void)corretButtonClick {
    
    CANCLELOCATING
    [self.locationService stopUserLocationService];
    
    // 返回拨键盘时被选定的行
    NSInteger row1 = [self.pickerView selectedRowInComponent:0];
    NSInteger row2 = [self.pickerView selectedRowInComponent:1];
    
    BVProvice *provice = self.cities[row1];

    // 获得选中的省份
    NSString *proviceName = provice.name;
    BVProvice *cit = _citys[row2];
    
    // 移除pickerView
    [self.citySelected removeFromSuperview];
    self.citySelected = nil;
    
    //如果还是当前城市，不执行任何操作 如果城市变动，将清空购物车
    if ([self.proviceName isEqualToString:proviceName] && [[[NSUserDefaults standardUserDefaults]objectForKey:@"city" ] isEqualToString:cit.name]) {
    
        //return;
    }else{
        [self removeAllShop]; //城市变动后，清空购物车
    }
    
    // 获得城市
    self.proviceName = proviceName;
    self.cityName = cit.name;

    // 默认地址
    self.navigationItem.rightBarButtonItem.title = self.cityName;
    
    // 存储当前城市信息
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.proviceName forKey:@"provice"];
    [defaults setObject:self.cityName forKey:@"city"];

    //县
    if(_xians.count > 0){
        NSInteger row3 = [self.pickerView selectedRowInComponent:2];
        BVProvice *xianM = _xians[row3];
        self.xianName = xianM.name;
        
        // 默认地址
        [defaults setObject:self.xianName forKey:@"dist"];
        self.navigationItem.rightBarButtonItem.title = self.xianName;
        
    }else{
        _xianName = nil;
         [defaults setObject:nil forKey:@"dist"];
        self.navigationItem.rightBarButtonItem.title = self.cityName;
    }
    
    _ProviceName = self.proviceName;
    _CityName = self.cityName;

    // 重新加载数据
    self.selectedLagerCategoryFlag = 0;
    self.selectedSmallCategoryFlag = 0;
    
    [self.shopsDetail removeAllObjects];
    [self.tableView reloadData];
    
    [self loadShopsData];
}

#pragma mark -- 取消
- (void)cancelButtonClick {
    // 移除pickerView
    [self.citySelected removeFromSuperview];
}

#pragma mark -- button点击事件
/**
 *  城市改变
 */
- (void)cityChangeClick {
    if(_citySelected) return;

    _citySelected = [[UIView alloc] init];
    _citySelected.frame = CGRectMake(0, BVScreenH - 250, BVScreenW, 250);
    _citySelected.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_citySelected];

    // 1.创建pickView
    UIPickerView *pickerView  = [[UIPickerView alloc] init];
    pickerView.y = _citySelected.height - 216;
    pickerView.width = BVScreenW;
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [_citySelected addSubview:pickerView];
    self.pickerView = pickerView;

    BVProvice *provice = self.cities[0];
    _citys = provice.cities;
    _xians = [NSArray array];
    [self.pickerView reloadComponent:1];
    [self.pickerView reloadComponent:2];
    
    // 2.添加工具条
    UIView *toolView = [[UIView alloc] init];
    toolView.frame = CGRectMake(0, 0, BVScreenW, 40);
    toolView.backgroundColor = BVBackgroundLightGrayColor;
    [_citySelected addSubview:toolView];

    UIButton *cancelButton = [[UIButton alloc] init];
    cancelButton.frame = CGRectMake(0, 0, 60, 40);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:BVBaseOrangeColor forState:UIControlStateNormal];
    cancelButton.titleLabel.font  = [UIFont systemFontOfSize:16];
    [cancelButton addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:cancelButton];

    UIButton *correctButton = [[UIButton alloc] init];
    correctButton.frame = CGRectMake(BVScreenW - 60, 0, 60, 40);
    [correctButton setTitle:@"确定" forState:UIControlStateNormal];
    [correctButton setTitleColor:BVBaseOrangeColor forState:UIControlStateNormal];
    correctButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [correctButton addTarget:self action:@selector(corretButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:correctButton];
}

/**
 *  取消按钮点击事件
 */
- (void)cancelBtnClick {
    [self.citySelected removeFromSuperview];
    self.citySelected = nil;
}
/**
 *  商品分类改变
 */
- (void)shopsCategoryDidChangeClick:(UIButton *)button {
    NSInteger categotyCount = 0;
    
    if (BVScreenH == 736) { // 6p
        categotyCount = 6;
    } else if (BVScreenH == 667) { // 6
        categotyCount = 5;
    } else if (BVScreenH == 568) { // 5/5s
        categotyCount = 5;
    } else if (BVScreenH == 480) { // 4/4s
        categotyCount = 4;
    }
    
    // 改变底部指示线的位置
    CGFloat shopCategoryW = BVScreenW / categotyCount;
    self.accessoryLine.x = shopCategoryW * button.tag;

    CGFloat scrollViewW = self.shopsCategoryScrollView.contentSize.width;
    CGFloat scrollViewCount = scrollViewW / BVScreenW;
    
    if (scrollViewCount > 0) { // 一个屏幕宽度显示不了
        if (BVScreenW * 0.5 < self.accessoryLine.x - shopCategoryW * 0.5 && (shopCategoryW - self.accessoryLine.x > BVScreenW * 0.5)) {
            self.shopsCategoryScrollView.contentOffset = CGPointMake(button.tag * shopCategoryW - BVScreenW * 0.5, 0);
        }
    }
    
    // 选中分类
    self.selectedLagerCategoryFlag = button.tag;

    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    // 获取左侧商品分类
    [self loadLeftShopsData];
}

/**
 *  商品小分类改变
 */
- (void)shopsCategoryDetailDidChangeClick:(UIButton *)button {
    self.selectedDetailButton.selected = NO;
    button.selected = YES;
    self.selectedDetailButton = button;

    // 小分类选中
    self.selectedSmallCategoryFlag = button.tag - 100;
    // 加载数据
    [self loadLeftShopsDetailData];
}

/**
 *  去结算
 */
- (void)correctOrderClick {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dict = [defaults objectForKey:@"USERINFO"];
    if (dict[@"id"] != nil) {
        BVCorrectOrderViewController *correctOrderVC = [[BVCorrectOrderViewController alloc] init];
        correctOrderVC.delegate = self;
        ROOTNAVPUSH(correctOrderVC, YES);
        [self.coverButton removeFromSuperview];
    } else {
        BVLoginViewController *loginVC = [[BVLoginViewController alloc] init];
        ROOTNAVPUSH(loginVC, YES);
    }
}

/**
 *  搜索商品
 */
- (void)searchShopsClick {
    BVSearchDetailShopsViewController *searchDetailVC = [[BVSearchDetailShopsViewController alloc] init];
    ROOTNAVPUSH(searchDetailVC, YES);
}

/**
 *  查看我的购物车
 */
- (void)shopCartButtonClick {
    // 清空数组
    [self.shopsCartCount removeAllObjects];
    
    // 商品数量
    [self.shopsCartCount addObjectsFromArray:[BVShopCart shopsCount:1]];
    // 设置tableview的尺寸
    self.shopCartTableView.x = 0;
    self.shopCartTableView.y = BVScreenH;
    self.shopCartTableView.width = BVScreenW;
    if (self.shopsCartCount.count < 8) {
        self.shopCartTableView.height = 44 * self.shopsCartCount.count + 30;
    } else {
        self.shopCartTableView.height = 44 * 8;
    }
    
    // 设置蒙板尺寸蒙板
    self.coverButton.x = 0;
    self.coverButton.y = 0;
    self.coverButton.width  = BVScreenW;
    self.coverButton.height = BVScreenH - 40 - self.shopCartTableView.height;
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    [window addSubview:_coverButton];

    NSTimeInterval duration = 0.3;
    [UIView animateWithDuration:duration animations:^{
        self.shopCartTableView.y = BVScreenH - 40 - self.shopCartTableView.height;
    }];
    
    // 刷新购物车tableview
    [self.shopCartTableView reloadData];
}

/**
 *   清空购物车
 */
- (void)deletedBtnClick:(UIButton *)button {
    
    // 移除萌版
    [self.coverButton removeFromSuperview];
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否清空购物车" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [MBProgressHUD showMessage:@"正在清空购物车"];
        // 从数据库中删除
        for (NSInteger i = 0; i < self.shopsCartCount.count; i++) {
            BVShopsLeftCategoryDetail *shopsLeftCategoryDetail = self.shopsCartCount[i];
            [BVShopCart deleteShopFromCart:shopsLeftCategoryDetail];
        }
        
        // 隐藏tableView
        NSTimeInterval duration = 0.25;
        [UIView animateWithDuration:duration animations:^{
            self.shopCartTableView.y = BVScreenH + 40 + self.shopCartTableView.height;
        }];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUD];
            // 设置底部购物车
            [self setupBaseInfo];
            [self.tableView reloadData];
            
        });

    }];
    
    UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:sureAction];
    [alertController addAction:cancleAction];
    
    DDMenuController *menuController = (DDMenuController*)((AppDelegate*)[[UIApplication sharedApplication] delegate]).menuController;
    
    [menuController presentViewController:alertController animated:YES completion:nil];
    
}

/**
 *  移除遮盖
 */
- (void)removeCoverClick:(UIButton *)button {
    [button removeFromSuperview];
    
    NSTimeInterval duration = 0.25;
    [UIView animateWithDuration:duration animations:^{
        self.shopCartTableView.y = BVScreenH + 40 + self.shopCartTableView.height;
    }];
}

#pragma mark -
#pragma mark - 添加商品
- (void)addShops:(UIButton *)button {
    // 左侧商品分类
    BVShopsCategoryDetail *shopsCategoryDetail = self.shopsDetail[self.selectedLagerCategoryFlag];
    
    // 左侧分类
    BVShopsLeftCategory *leftCategory = shopsCategoryDetail.child_list[self.selectedSmallCategoryFlag];

    BVShopsLeftCategoryDetail *detailShops = leftCategory.breed_list[button.tag - 200];
    
    if ([BVShopCart lookUpShop:detailShops] != nil) { // 商品已在购物车
        // 修改商品
        [BVShopCart modifyShopToCartL:detailShops count:[BVShopCart shopCartCounts:detailShops] + 1];
    } else { // 新商品
        // 添加商品到数据库
        [BVShopCart addShopToCart:detailShops];
    }
    
    // 显示购物车标识
    self.shopCartFlag.hidden = NO;
    [self.shopCartButton addSubview:self.shopCartFlag];
    [button buttonClickAnimation:self.shopCartFlag];
    
    // 当前选中商品数量
    NSInteger selectedShops  = [self.shopCartFlag.currentTitle integerValue] + 1;

    // 显示商品数量
    [self.shopCartFlag setTitle:[NSString stringWithFormat:@"%ld", (long)selectedShops] forState:UIControlStateNormal];

    // 设置购物车基本信息
    [self setupBaseInfo];

    // 刷新tableview
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag - 200 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark -
#pragma mark - 减少商品
- (void)subShops:(UIButton *)button {
    // 左侧商品分类
    BVShopsCategoryDetail *shopsCategoryDetail = self.shopsDetail[self.selectedLagerCategoryFlag];
    
    // 左侧分类
    BVShopsLeftCategory *leftCategory = shopsCategoryDetail.child_list[self.selectedSmallCategoryFlag];

    BVShopsLeftCategoryDetail *detailShops = leftCategory.breed_list[button.tag - 500];

    // 当前选中商品数量
    NSInteger selectedShops = [BVShopCart shopCartCounts:detailShops] - 1;
    
    if (selectedShops > 0) {
        // 修改商品
        [BVShopCart modifyShopToCartL:detailShops count:selectedShops];
        
        // 显示商品数量
        [self.shopCartFlag setTitle:[NSString stringWithFormat:@"%ld", (long)selectedShops] forState:UIControlStateNormal];
        [button buttonClickAnimation:self.shopCartFlag];
        
    } else {
        [button buttonClickAnimation:self.shopCartFlag];
        // 从数据库删除数据
        [BVShopCart deleteShopFromCart:detailShops];
    }
    
    // 设置购物车基本信息
    [self setupBaseInfo];
    
    // 刷新tableview
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag - 500 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark -- 获取我的订单
//获取我的订单
- (void)loadMyOrderData{
    //拼接路径
    NSString *url = [NSString stringWithFormat:@"%@/VegetablePayAPI/getMyOrderListById",Path];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSDictionary *userinfo = [defaults objectForKey:@"USERINFO"];
    
    NSString *user_id = userinfo[@"id"];
    
    if (user_id != nil) {
        params[@"user_id"] = user_id;
        //        SHOWLOADING
        [HttpTool get:url params:params success:^(id responseObj) {
            //            HIDELOADING
            if ([responseObj[@"code"] isEqualToString:@"1"]) {
                NSArray *array = [BVMyOrder objectArrayWithKeyValuesArray:responseObj[@"data"]];
                [self.orders addObjectsFromArray:array];
                order = self.orders[0];
                [self setupBaseInfo];
                
                //                [self.tableView reloadData];
            } else {
                //                ERROR(@"没有订单信息");
                order = nil;
                [self setupBaseInfo];
             
            }
        } failure:^(NSError *error) {
        //            NETWORKINGERROR
        }];
    }else{
        order = nil;
        [self setupBaseInfo];
    }
}

#pragma mark -- 获取当前时间
//获取当前时间
- (NSString *)getCurrentTime{
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

#pragma  mark -- 设置购物车基本信息
- (void)setupBaseInfo {

    // 清空数组
    [self.shopsCartCount removeAllObjects];
    self.balanceButton.backgroundColor = BVColor(153, 153, 153);
    
    NSString *nowDate = [self getCurrentTime];
    
    NSString *orderDate = [order.order_time substringToIndex:10];
    
    // 商品数量
    [self.shopsCartCount addObjectsFromArray:[BVShopCart shopsCount:1]];
    
    if (self.shopsCartCount.count != 0) {
        self.shopCartFlag.hidden = NO;
        CGFloat allPrice   = 0.0;
        NSInteger allShops = 0;
        for (NSInteger i = 0; i < self.shopsCartCount.count; i++) {
            BVShopsLeftCategoryDetail *detail = self.shopsCartCount[i];
            
            allPrice += [BVShopCart shopCartCounts:detail] * [detail.each_bag_money floatValue];
            
            if ([nowDate isEqualToString:orderDate] && allPrice > 0.0) {
                
                [self.balanceButton setTitle:@"去结算" forState:UIControlStateNormal];
                self.balanceButton.backgroundColor = BVBaseOrangeColor;
                
                self.balanceButton.enabled = YES;
                
            } else {
            
            if ([self.minMoney floatValue] - allPrice > 0.0) {
                [self.balanceButton setTitle:[NSString stringWithFormat:@"还差%.2f元", [self.minMoney floatValue] - allPrice] forState:UIControlStateNormal];
                
            } else {
                [self.balanceButton setTitle:@"去结算" forState:UIControlStateNormal];
                self.balanceButton.backgroundColor = BVBaseOrangeColor;
                self.balanceButton.enabled = YES;
            }
            }
            allShops += [BVShopCart shopCartCounts:detail];
        }
        
        // 商品总个数
        [self.shopCartFlag setTitle:[NSString stringWithFormat:@"%ld", (long)allShops] forState:UIControlStateNormal];
        
        // 已选商品总价格
        self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f", allPrice];
        
        self.shopCartButton.enabled = YES;
        
        if (allShops == 0) { // 没有选择商品
            self.shopCartFlag.hidden = YES;
            self.priceLabel.text = @"¥0.00";
            self.balanceButton.enabled  = NO;
            self.shopCartButton.enabled = NO;
        }
       
    } else {
        self.shopCartFlag.hidden = YES;
        if (order != nil && [nowDate isEqualToString:orderDate]) {
//            NSLog(@"有订单");
            self.priceLabel.text = @"¥0.00";
            self.balanceButton.enabled  = NO;
            self.shopCartButton.enabled = NO;
            [self.balanceButton setTitle:[NSString stringWithFormat:@"去结算"] forState:UIControlStateNormal];
        } else {
//            NSLog(@"无订单");
            self.priceLabel.text = @"¥0.00";
            self.balanceButton.enabled  = NO;
            self.shopCartButton.enabled = NO;
            [self.balanceButton setTitle:[NSString stringWithFormat:@"还差%@元", self.minMoney] forState:UIControlStateNormal];
        }
    }
}

#pragma mark -- 购物车tableview中商品的添加和减少
/**
 *  添加商品
 */
- (void)addBtnClick:(UIButton *)btn {
    BVShopsLeftCategoryDetail *shopDetail = self.shopsCartCount[btn.tag - 300];

    // 修改商品
    [BVShopCart modifyShopToCartL:shopDetail count:[BVShopCart shopCartCounts:shopDetail] + 1];

    // 当前选中商品数量
    NSInteger selectedShops = [self.shopCartFlag.currentTitle integerValue] + 1;

    // 显示商品数量
    [self.shopCartFlag setTitle:[NSString stringWithFormat:@"%ld", (long)selectedShops] forState:UIControlStateNormal];

    // 设置购物车基本信息
    [self setupBaseInfo];

    // 刷新tableview
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag - 300 inSection:0];
    [self.shopCartTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

    [self.tableView reloadData];
}

/**
 *  减少商品
 */
- (void)subBtnClick:(UIButton *)btn {
    BVShopsLeftCategoryDetail *shopDetail = self.shopsCartCount[btn.tag - 600];

    // 当前选中商品数量
    NSInteger selectedShops = [BVShopCart shopCartCounts:shopDetail] - 1;
    
    if (selectedShops > 0) {
        // 修改商品
        [BVShopCart modifyShopToCartL:shopDetail count:selectedShops];
        
        // 显示商品数量
        [self.shopCartFlag setTitle:[NSString stringWithFormat:@"%ld", (long)selectedShops] forState:UIControlStateNormal];
    } else {
        // 从数据库删除数据
        [BVShopCart deleteShopFromCart:shopDetail];
        
        // 从数组中删除
        [self.shopsCartCount removeObjectAtIndex:btn.tag - 600];
        
        // 设置tableview的尺寸
        if (self.shopsCartCount.count < 8) {
            self.shopCartTableView.height = 44 * self.shopsCartCount.count;
            self.shopCartTableView.y      = BVScreenH - self.shopCartTableView.height - 40;
        } else {
            self.shopCartTableView.height = 44 * 8;
        }
        
        // 设置蒙板尺寸蒙板
        self.coverButton.x = 0;
        self.coverButton.y = 0;
        self.coverButton.width = BVScreenW;
        self.coverButton.height = BVScreenH - 40 - self.shopCartTableView.height;
    }
    
    if (self.shopsCartCount.count == 0) { // 没有商品
        [self.coverButton removeFromSuperview];
    }
    
    // 设置购物车基本信息
    [self setupBaseInfo];
    
    // 刷新
    [self.shopCartTableView reloadData];
    
    [self.tableView reloadData];
}

#pragma mark -- BMKLocationServiceDelegate
#pragma mark ---百度地图服务
/**
 *  用户位置更新后，会调用此函数
 *  @param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    [self reverseGeocode:userLocation];
    [self.locationService stopUserLocationService];
}

/**
 * 反地理编码: 经纬度－》地名
 */
- (void)reverseGeocode:(BMKUserLocation *)userLocation {
    // 2.开始反地理编码
    CLLocation *location = [[CLLocation alloc] initWithLatitude:userLocation.location.coordinate.latitude longitude:userLocation.location.coordinate.longitude];
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error || placemarks.count == 0) {
            LOCATINGFAILE
        } else { // 找到具体位置

            for (CLPlacemark *placemark in placemarks) {
                //对定位到的地址进行带 省 市 字的过虑
                NSString *city1;
                if ([placemark.administrativeArea containsString:@"省"]) {
                    city1 = [placemark.administrativeArea stringByReplacingOccurrencesOfString:@"省" withString:@""];
                }else{
                    city1 = placemark.administrativeArea;
                }
                
                NSString *city2;
                if ([placemark.locality containsString:@"市"]) {
                    city2 = [placemark.locality stringByReplacingOccurrencesOfString:@"市" withString:@""];
                }else{
                    city2 = placemark.locality;
                }
                
                self.proviceName = city1;
                self.cityName = city2;
                self.xianName = placemark.subLocality;
            }

            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

            if (self.proviceName!= nil && self.cityName != nil) {
                // 将第一次获取到的地址存入缓存
                [defaults setObject:self.proviceName forKey:@"provice"];
                [defaults setObject:self.cityName forKey:@"city"];
                [defaults setObject:self.xianName forKey:@"dist"];
                // 加载商品数据
                [self loadShopsData];
                self.navigationItem.rightBarButtonItem.title = self.cityName;
            }
            if (self.xianName.length != 0) {
                self.navigationItem.rightBarButtonItem.title = self.xianName;
                [defaults setObject:self.xianName forKey:@"dist"];
            }
        }
        CANCLELOCATING
    }];
}

#pragma mark -- BVCorrectOrderViewControllerDelegate
- (void)correctOrderViewControllerDidFinish:(BVCorrectOrderViewController *)correctOrder {
    if (self.shopCartTableView.y < BVScreenH && self.shopCartTableView.y != 0.0) {
        // 设置蒙板尺寸蒙板
        self.coverButton.x = 0;
        self.coverButton.y = 0;
        self.coverButton.width  = BVScreenW;
        self.coverButton.height = BVScreenH - 40 - self.shopCartTableView.height;
        UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
        [window addSubview:_coverButton];
    }
}
#pragma mark  付款结束通知，清空购物车显示信息
- (void)cleanShopCar:(NSNotification *)notify{
    [self setupBaseInfo];
}
#pragma mark  修改商品数量的通知
- (void)reloadTableViewAndBaseCart{

    [self.tableView reloadData];
    [self setupBaseInfo];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
